package com.mapapp.flutter_mapapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;


public class TitleBar extends FrameLayout {

    private ImageView ivBack;
    private ImageView ivRight;
    private TextView titleBarCenter;
    private TextView titleBarRight;

    private boolean hasBottomLine;
    private boolean hasBack;
    private String rightText;
    private String centerText;
    private Drawable rightSrc;
    private Drawable leftSrc;

    public ImageView getIvBack() {
        return ivBack;
    }

    public TextView getTitleBarCenter() {
        return titleBarCenter;
    }

    public TextView getTitleBarRight() {
        return titleBarRight;
    }

    public ImageView getIvRight() {
        return ivRight;
    }

    public TitleBar(@NonNull Context context) {
        this(context, null, 0);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBar, defStyleAttr, 0);
        hasBack = a.getBoolean(R.styleable.TitleBar_titleBarHasBack, false);
        rightText = a.getString(R.styleable.TitleBar_titleBarRightText);
        rightSrc = a.getDrawable(R.styleable.TitleBar_titleBarRightSrc);
        leftSrc = a.getDrawable(R.styleable.TitleBar_titleBarLeftSrc);
        centerText = a.getString(R.styleable.TitleBar_titleBarCenterText);
        hasBottomLine = a.getBoolean(R.styleable.TitleBar_titleBarBottomLine, false);
        a.recycle();

        init();
    }

    private void init() {
        View rootView = inflate(getContext(), R.layout.widget_title_bar, this);
        ivBack = rootView.findViewById(R.id.iv_back);
        titleBarCenter = rootView.findViewById(R.id.title_bar_center);
        titleBarRight = rootView.findViewById(R.id.title_bar_right);
        ivRight = rootView.findViewById(R.id.iv_right);
        View viewBottomLine = rootView.findViewById(R.id.view_bottom_line);

        if (!TextUtils.isEmpty(rightText)) {
            titleBarRight.setText(rightText);
        }

        if (!TextUtils.isEmpty(centerText)) {
            titleBarCenter.setText(centerText);
        }

        ivBack.setVisibility(leftSrc == null ? hasBack ? VISIBLE : INVISIBLE : VISIBLE);

        viewBottomLine.setVisibility(hasBottomLine ? VISIBLE : INVISIBLE);

        if (rightSrc != null) {
            ivRight.setImageDrawable(rightSrc);
        }

        if (leftSrc != null) {
            ivBack.setImageDrawable(leftSrc);
        }
    }

}
